package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	res, err := CopyFile("target.txt", "output.txt")
	if err == nil {
		fmt.Println("复制完成！")
	} else {
		fmt.Println(err, res)
	}

}

func CopyFile(dstName, srcName string) (written int64, err error) {
	src, err := os.Open(srcName)
	if err != nil {
		return
	}

	defer src.Close()

	dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		return
	}

	defer dst.Close()

	return io.Copy(dst, src)
}
